package ex;

import org.apache.commons.math3.util.Combinations;
import org.apache.commons.math3.util.CombinatoricsUtils;

import java.util.Arrays;
import java.util.Iterator;

/**
 * @Description 组合工具类
 * @Date 2022/3/19 21:05
 * @Author eric ling
 */
public class CombinationUtil {
    public static void main(String[] args) {
        // DSP(51,26,16,16,8)
        System.out.println(CombinatoricsUtils.binomialCoefficient(51,16));
    }

    public static int[][] allCombList(int n, int k) {
        // 计算有多少种组合
        long count = CombinatoricsUtils.binomialCoefficient(n, k);
        // 创建对应大小的二维数组 第一维：组合数 第二维：对应的n个数
        int[][] idxs = new int[Math.toIntExact(count)][k];
        Combinations coms = new Combinations(n, k);
        Iterator<int[]> iterator = coms.iterator();
        int tmpIdx = 0;
        while (iterator.hasNext()) {
            int[] next = iterator.next();
            idxs[tmpIdx] = Arrays.copyOf(next, k);
            tmpIdx++;
        }
        return idxs;
    }
}
